package org.lztvn.elearning.front_controllers;


import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.lztvn.elearning.controllers.AppController;
import org.lztvn.elearning.entities.Question;
import org.lztvn.elearning.entities.StudentTest;
import org.lztvn.elearning.entities.Test1;
import org.lztvn.elearning.entities.User;
import org.lztvn.elearning.service.ICourseService;
import org.lztvn.elearning.service.IQuestionService;
import org.lztvn.elearning.service.IStudentTestService;
import org.lztvn.elearning.service.ITestService;
import org.lztvn.elearning.service.IUserService;
import org.lztvn.elearning.service.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.w3c.dom.ls.LSInput;

@Controller
@RequestMapping(value = "/listTest")
@SessionAttributes("userFLogin")
public class TestForStudentController extends AppController {
	@Autowired
	ICourseService serviceCor;
	@Autowired
	IUserService serviceUser;
	@Autowired
	ITestService serviceTest;
	@Autowired
	IQuestionService serviceQuest; 
	@Autowired
	IStudentTestService serviceStudentTest;
	
	Utility util;

	private void init() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		this.util = (Utility) context.getBean("util");
	}
	
	@RequestMapping(value = "/alltests")
	public String index(Model model) {
		model.addAttribute("tests", serviceTest.getlistTest());
		return "join_test";
	}

	@RequestMapping(value = "/test")
	public ModelAndView viewTest(Long idTest, Model model) {
		Test1 t = serviceTest.getTestbyID(idTest);
		model.addAttribute("test", t);
		model.addAttribute("listquest", serviceQuest.getlistQuestionbyTestId_bis(idTest));
		return new ModelAndView("test");
	}
	
	@RequestMapping(value = "/view_test_correction")
	public ModelAndView view_test_correction(Long idTest, Model model) {
		Test1 t = serviceTest.getTestbyID(idTest);
		model.addAttribute("test", t);
		model.addAttribute("view_correction", "view");
		model.addAttribute("listquest", serviceQuest.getlistQuestionbyTestId_bis(idTest));
		return new ModelAndView("test");
	}
	
	@RequestMapping(value = "/do_test",method = RequestMethod.POST)
	public ModelAndView flogintest(Model model,WebRequest webRequest,HttpSession session, 
			                  final RedirectAttributes redirectAttributes) {
		Long idTest = Long.valueOf(webRequest.getParameter("idTest"));
		
		redirectAttributes.addFlashAttribute("idTest", idTest);
		
		return new ModelAndView("redirect:login");
	}
	
	@RequestMapping(value = "/login")
	public String login(Model model, HttpSession session) {
		//, @ModelAttribute("idCourse") final Long idCourse
		init();
		if (util.checkFrontSession(session) == true) {
			return ("welcome");
		}
		model.addAttribute("user", new User());
		return ("flogin");
	}
	
	@RequestMapping(value="saveTest", method=RequestMethod.POST)
	public String saveTest(Model model, WebRequest webRequest,  HttpSession session){
		Long idtest = Long.parseLong(webRequest.getParameter("idtest"));
		User u = (User)session.getAttribute("userFLogin");
		Long iduser = u.getIdUser();
		List<Question> listques = serviceQuest.getlistQuestionbyTestId_bis(idtest);
		Integer nbreQues = listques.size();
		Integer nb_right_ans =0;
		Hashtable<Long, Integer> listres = new Hashtable<Long, Integer>();
		for(Question  quest:listques){
			Long idq = quest.getQestId();
			Integer answer = quest.getAnswer();
			String param = "choice"+idq;
			String choice = webRequest.getParameter(param);
			
			if(!choice.equals(null)){
				if(answer== Integer.parseInt(choice)){
					nb_right_ans++;
				}
			}
			listres.put(idq, Integer.parseInt(choice));
		}
		
		double res = (nb_right_ans*100)/nbreQues;
		StudentTest s = new StudentTest(new Date(), res);
		s.setStudent(u);
		s.setMytest(serviceTest.getTestbyID(idtest));
		model.addAttribute("result", res);
		model.addAttribute("idtest", idtest);
        serviceStudentTest.addStudentTest(s);
		
		return "result";
	}

	
	
	// authentication
	@RequestMapping(value = "/authentest", method = RequestMethod.POST)
	public String login(String username, String password, ModelMap model,
			HttpServletRequest request,WebRequest webRequest, RedirectAttributes redirectAttributes) throws IOException {
		if (serviceUser.login(username, password).size() > 0) {
			User u = serviceUser.getUserByUsername(username);
			model.addAttribute("userFLogin", u);
			request.getSession().setAttribute("userFLogin", u);
			String idTest = webRequest.getParameter("idTest");
			if(idTest==null||idTest=="")
		   	  return ("redirect:welcome");
			else{
				Test1 t = serviceTest.getTestbyID(Long.parseLong(idTest, 10));
				model.addAttribute("listquest", serviceQuest.getlistQuestionbyTestId(t.getId()));
				model.addAttribute("test", t);
			
				
				return ("test");
			}
		} else {
			model.addAttribute("message", "Sorry, username or password error");
			return ("redirect:login");
		}
	}

}
